const app = getApp()

export default {
	name: 'list-loader',
	data(){
		return {
			count: 10,						// 分页加载的每页的数量
		}
	},
	methods:{
		// 下拉刷新
		refreshRecord(list, url, extra={}){
			uni.showLoading({
				title:'加载中...'
			})
			uni.request({
				header:{
					Authorization: 'Bearer '+app.globalData.token,
				},
				url,
				method: 'GET',
				data: {
					page: 1,
					size: list.list.length || 10,
					...extra,
				},
				success: res => {
					const {data} = res
					console.log(data)
					uni.hideLoading()
					if(data.code==200){
						list.pageNum = 1
						const d = data.data
						list.totalPage=d.totalPage
						list.total=d.total
						// 直接重置数组
						list.list=data.data.list
						uni.hideLoading()
						uni.stopPullDownRefresh()
					}else if(data.code==500){
						uni.showToast({
							icon:'none',
							title: data.message,
						})
					}
				},
				fail: (err) => {console.error(err)},
			});
		},
		// 拉取早起的历史数据
		pullNewRecord(list, url, extra={}){
			if(list.list.length>=list.total){
				return
			}
			uni.showLoading({
				title:'加载中...'
			})
			uni.request({
				header:{
					Authorization: 'Bearer '+app.globalData.token,
				},
				url,
				method: 'GET',
				data: {
					page: ++list.pageNum,
					size: list.pageSize || 10,
					...extra,
				},
				success: res => {
					const {data} = res
					if(data.code==200){
						console.log(data)
						uni.hideLoading()
						const d = data.data
						list.totalPage=d.totalPage
						list.total=d.total
						uni.hideLoading()
						if(d.total>list.list.length){
							if(d.list.length>0){
								list.list.push(...d.list)
							}
						}
					}else if(data.code==500){
						uni.showToast({
							icon:'none',
							title: data.message,
						})
					}
				},
				fail: (err) => {console.error(err)},
			});
		},
	},
	computed:{
		// 返回当前列表的计算表达
		curList(){
			
		}
	}
}